#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@desc: .

@Project：kuberCloudTest 
@File：vx_lan_pool.py
@Author：admin
@Date：2024/4/16 14:10 
"""

from kuberCloudTest.common.basic import basicUtils
from kuberCloudTest.common.constant import OFFSET, LIMIT, AsyncType
from kuberCloudTest.common.utils import _getOperationType
from kuberCloudTest.networks.urls import networkUrls


class vxLanPoolUtils(basicUtils):

    def __init__(self, accessToken):
        super().__init__(accessToken)


def get_utils(accessToken):
    return vxLanPoolUtils(accessToken=accessToken)


def getVXLanPool(accessToken, **kwargs):
    """
    获取vxLan池
    :param accessToken:
    :param kwargs: limit:
                   offset:
                   keyword: xxx
                   searchItem: 0: vxLan池名称; 1: vxLan池uuid
    :return:
    """

    return get_utils(accessToken).cloud_http_request.get(networkUrls.getVXLanPool.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'keyword': kwargs.get('keyword', ''),
                                                             'searchitem': kwargs.get('searchItem', 0),
                                                         })


def createVXLanPool(accessToken, vxLanPoolName, vniStart: int, vniEnd: int, clusterId=None, vtepCidr=None, remark=''):
    """
    创建vxLan池
    :param accessToken:
    :param vxLanPoolName: vxLan池名称
    :param vniStart: 起始Vni
    :param vniEnd: 结束Vni
    :param clusterId: 集群UUID，注意：clusterId与vtepCidr同时传参有效
    :param vtepCidr: VTEP CIDR，注意：clusterId与vtepCidr同时传参有效
    :param remark: 描述
    :return:
    """
    operationType = 5001
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['vxlanpoolname'] = vxLanPoolName
    data['remark'] = remark
    data['vnistart'] = vniStart
    data['endvni'] = vniEnd

    if clusterId and vtepCidr:
        data['clusterid'] = clusterId
        data['vtepcidr'] = vtepCidr

    return get_utils(accessToken).cloud_http_request.post(networkUrls.createVXLanPool.value, data)


def deleteVXLanPool(accessToken, vxLanPoolIds: str):
    """
    删除vxLan池
    :param accessToken:
    :param vxLanPoolIds: vxLan池UUID(多个使用英文逗号分割)
    :return:
    """
    operationType = 5003
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['vxlanpoolids'] = vxLanPoolIds
    return get_utils(accessToken).cloud_http_request.post(networkUrls.deleteVXLanPool.value, data)


def updateVXLanPool(accessToken, vxLanPoolId, vxLanPoolName, remark=''):
    """
    更新vxLan池
    :param accessToken:
    :param vxLanPoolId: vxLan池UUID
    :param vxLanPoolName: vxLan池名称
    :param remark: 描述
    :return:
    """
    operationType = 5002
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['vxlanpoolid'] = vxLanPoolId
    data['vxlanpoolname'] = vxLanPoolName
    data['remark'] = remark

    return get_utils(accessToken).cloud_http_request.post(networkUrls.updateVXLanPool.value, data)


def getVXLanPoolCluster(accessToken, state: int = 1, **kwargs):
    """
    查询集群（创建VXLanPool时需用）
    :param accessToken:
    :param state: 集群的状态，1：启用；0：停用
    :param kwargs: limit:
                   offset:
                   keyword: xxx
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getVXLanPoolCluster.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'keyword': kwargs.get('keyword', ''),
                                                             'state': state,
                                                         })


def getVXLanPoolLoadCluster(accessToken, vxLanPoolId, **kwargs):
    """
    查询vxLan池已加载的集群
    :param vxLanPoolId: vxLan池UUID
    :param accessToken:
    :param kwargs: limit:
                   offset:
                   keyword: xxx
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getVXLanPoolLoadCluster.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'keyword': kwargs.get('keyword', ''),
                                                             'vxlanpoolid': vxLanPoolId,
                                                         })


def getVXLanPoolUnloadCluster(accessToken, vxLanPoolId, **kwargs):
    """
    查询vxLan池未加载的集群
    :param vxLanPoolId: vxLan池UUID
    :param accessToken:
    :param kwargs: limit:
                   offset:
                   keyword: xxx
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getVXLanPoolUnloadCluster.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'keyword': kwargs.get('keyword', ''),
                                                             'vxlanpoolid': vxLanPoolId,
                                                         })


def loadClusterByVXLanPool(accessToken, vxLanPoolId, clusterId, vtepCidr):
    """
    加载集群到vxLan池
    :param accessToken:
    :param vxLanPoolId: vxLan池UUID
    :param clusterId: 集群UUID
    :param vtepCidr: VTEP CIDR
    :return:
    """
    operationType = 5004
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['vxlanpoolid'] = vxLanPoolId
    data['clusterid'] = clusterId
    data['vtepcidr'] = vtepCidr

    return get_utils(accessToken).cloud_http_request.post(networkUrls.loadClusterByVXLanPool.value, data)


def unloadClusterByVXLanPool(accessToken, vxLanPoolId, clusterId):
    """
    卸载集群到vxLan池
    :param accessToken:
    :param vxLanPoolId: vxLan池UUID
    :param clusterId: 集群UUID
    :return:
    """
    operationType = 5005
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['vxlanpoolid'] = vxLanPoolId
    data['clusterId'] = clusterId

    return get_utils(accessToken).cloud_http_request.post(networkUrls.unloadClusterByVXLanPool.value, data)


def getL2NetworkByVXLanPool(accessToken, vxLanPoolId, **kwargs):
    """
    获取vxLan池下的L2网络
    :param accessToken:
    :param vxLanPoolId: vxLan池UUID
    :param kwargs: limit:
                   offset:
                   keyword: xxx
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getL2NetworkByVXLanPool.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'keyword': kwargs.get('keyword', ''),
                                                             'vxlanpoolid': vxLanPoolId,
                                                         })


def globalShareVXLanPool(accessToken, vxLanPoolIds: str):
    """
    全局共享vxLan池
    :param accessToken:
    :param vxLanPoolIds: vxLan池UUID(多个使用英文逗号分割)
    :return:
    """
    operationType = 5009
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = vxLanPoolIds

    return get_utils(accessToken).cloud_http_request.post(networkUrls.globalShareVXLanPool.value, data)


def globalRecallVXLanPool(accessToken, vxLanPoolIds: str):
    """
    全局召回vxLan池
    :param accessToken:
    :param vxLanPoolIds: vxLan池UUID(多个使用英文逗号分割)
    :return:
    """
    operationType = 5010
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = vxLanPoolIds

    return get_utils(accessToken).cloud_http_request.post(networkUrls.globalRecallVXLanPool.value, data)


def createVXLanL2Network(accessToken, vxLanPoolId, vniName, vniStart: int, vniEnd: int):
    """
    创建vxLan L2网络
    :param accessToken:
    :param vxLanPoolId: vxLan池UUID
    :param vniName: VNI名称
    :param vniStart: VNI起始值
    :param vniEnd: VNI结束值
    :return:
    """
    pass
    # TODO: 接口暂时有问题


def deleteVXLanL2Network(accessToken, vxLanPoolId, vniName):
    """
    删除vxLan L2网络
    :param accessToken:
    :param vxLanPoolId: vxLan池UUID
    :param vniName: VNI名称
    :return:
    """
    pass
    # TODO: 接口暂时有问题


def getVXLanPoolVin(accessToken, vxLanPoolId, **kwargs):
    """
    获取vxLan池下的Vin
    :param accessToken:
    :param vxLanPoolId: vxLan池UUID
    :param kwargs: limit:
                   offset:
                   keyword: xxx
    :return:
    """

    return get_utils(accessToken).cloud_http_request.get(networkUrls.getVXLanPoolVin.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'keyword': kwargs.get('keyword', ''),
                                                             'vxlanpoolid': vxLanPoolId,
                                                         })


def createVXLanPoolVin(accessToken, vxLanPoolId, vniName, vniStart: int, vniEnd: int):
    """
    创建vxLan Vin段
    :param accessToken:
    :param vxLanPoolId: vxLan池UUID
    :param vniStart: 起始Vni
    :param vniEnd: 结束Vni
    :param vniName: vni名称
    :return:
    """
    operationType = 5007
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['vxlanpoolid'] = vxLanPoolId
    data['vniname'] = vniName
    data['vnistart'] = vniStart
    data['endvni'] = vniEnd

    return get_utils(accessToken).cloud_http_request.post(networkUrls.createVXLanPoolVin.value, data)


def updateVXLanPoolVin(accessToken, vxLanPoolId, vniId, vniName, vniStart: int, vniEnd: int):
    """
    更新vxLan Vin段
    :param accessToken:
    :param vxLanPoolId: vxLan池UUID
    :param vniStart: 起始Vni
    :param vniEnd: 结束Vni
    :param vniName: vni名称
    :return:
    """
    operationType = 5006
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['vxlanpoolid'] = vxLanPoolId
    data['vniid'] = vniId
    data['vniname'] = vniName
    data['vnistart'] = vniStart
    data['endvni'] = vniEnd

    return get_utils(accessToken).cloud_http_request.post(networkUrls.updateVXLanPoolVin.value, data)


def deleteVXLanPoolVin(accessToken, vxLanPoolId, vniIds: str):
    """
    删除vxLan Vin段
    :param accessToken:
    :param vxLanPoolId: vxLan池UUID
    :param vniIds: vniUUID(多个使用英文逗号分割)
    :return:
    """
    operationType = 5008
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['vxlanpoolid'] = vxLanPoolId
    data['vniIds'] = vniIds

    return get_utils(accessToken).cloud_http_request.post(networkUrls.deleteVXLanPoolVin.value, data)
